<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>基于边缘计算的居家健康与安全管家系统</title>
    <style>
        :root {
            --primary-color: #4facfe;
            --glass-bg: rgba(255, 255, 255, 0.2);
            --glass-border: rgba(255, 255, 255, 0.3);
        }

        * {
            padding: 0;
            margin: 0;
            box-sizing: border-box;
        }

        html,
        body {
            height: 100%;
            font-family: 'Microsoft YaHei', sans-serif;
        }

        body {
            display: flex;
            justify-content: center;
            align-items: center;
            background: url('333.png') center/cover no-repeat fixed;
        }

        .dashboard {
            width: 400px;
            background: var(--glass-bg);
            border-radius: 8px;
            backdrop-filter: blur(10px);
            border: 1px solid var(--glass-border);
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.6);
            padding: 20px;
            margin: 20px;
        }

        .title {
            font-size: 24px;
            text-align: center;
            color: #333;
            margin-bottom: 15px;
        }

        .data-panel {
            display: grid;
            gap: 15px;
            margin-bottom: 20px;
        }

        /* .data-item {
            
            border-radius: 5px;
            padding: 12px;
            border: 1px solid rgba(0, 0, 0, 0.1);
        } */

        .data-label {
            color: #666;
            font-size: 14px;
            margin-bottom: 8px;
        }

        input[type="text"] {
            width: 100%;
            height: 40px;
            padding: 0 10px;
            border: 1px solid rgba(0, 0, 0, 0.1);
            border-radius: 5px;
            background: rgba(255, 255, 255, 0.9);
            font-size: 14px;
        }

        .btn {
            width: 100%;
            height: 40px;
            background: rgba(50, 50, 50, 0.8);
            color: #f1ecec;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            transition: all 0.3s;
            font-size: 16px;
            margin: 15px 0;
        }

        .btn:hover {
            background: rgba(50, 50, 50, 0.9);
        }

        .nav-links {
            display: flex;
            justify-content: space-between;
            margin-top: 20px;
            padding: 10px 0;
            border-top: 1px solid rgba(255, 255, 255, 0.2);
        }

        .nav-links a {
            color: #666;
            text-decoration: none;
            font-size: 14px;
            transition: color 0.3s;
        }

        .nav-links a:hover {
            color: #333;
        }
        .refresh-interval {
            width: 100%;
            /* 改为100%宽度 */
            height: 40px;
            /* 高度与温湿度输入框一致 */
            padding: 0 10px;
            border: 1px solid rgba(0, 0, 0, 0.1);
            border-radius: 5px;
            background: rgba(255, 255, 255, 0.9);
            font-size: 14px;
        }

        /* 添加容器宽度限制 */
        .auto-refresh-controls {
            display: flex;
            gap: 10px;
            align-items: center;
        }
    </style>
    <script type="text/javascript">
        var refreshIntervalId = null; // 存储定时器ID
        var currentInterval = 3000; // 默认上报间隔3秒

        function init() {
            // 从本地存储获取保存的上报间隔
            const savedInterval = localStorage.getItem('healthRefreshInterval');
            if (savedInterval) {
                currentInterval = parseInt(savedInterval);
                document.getElementById('refreshInterval').value = currentInterval / 1000;
            }

            refresh();
            startAutoRefresh(); // 启动自动上报
        }
        function refresh() {
            var xhr = new XMLHttpRequest(); // 创建XMLHttpRequest对象
            var heartpulse = document.getElementsByName("heartpulse");    // 定义一个数组来存储输入框元素
            var lightpower = document.getElementsByName("lightpower");
            var bloodoxygen = document.getElementsByName("bloodoxygen");
            var url = ""; // 请求的URL
            xhr.open("POST", url, true); // 配置请求：POST方法、URL、异步请求
            var post_req = {
                "action": "read",
                "sensors": [201, 202, 203],
            };
            xhr.onreadystatechange = function () {  // 监听状态变化
                if (xhr.readyState === 4 && xhr.status === 200) { // 请求完成且成功
                    var response = xhr.responseText; // 获取响应数据----------------------------------------------
                    console.log(response);
                    var info = JSON.parse(response)  //将json字符串反序列化----------------------------------------
                    //console.log(info.modbus); // 在浏览器控制台打印响应数据
                    //没有进入for循环，因为info.modbus是一个对象，而不是数组。
                    // 要遍历对象的属性，可以使用for...in循环，或者使用Object.keys()方法获取属性名数组。
                    // 例如，使用for...in循环遍历对象的属性：
                    console.log(info.modbus);

                    // 更新最后上报时间
                    document.getElementById('lastRefreshTime').textContent = new Date().toLocaleTimeString();
                    for (var i in info.modbus) {    // 遍历数组中的每个元素
                        console.log(info.modbus[i].key);
                        if (info.modbus[i].key == 201) {  // 根据key值判断数据类型
                            console.log(info.modbus[i].value);
                            document.getElementsByName('heartpulse')[0].value = info.modbus[i].value;  // 更新输入框的值
                        } else if (info.modbus[i].key == 202) {
                            console.log(info.modbus[i].value);
                            document.getElementsByName('lightpower')[0].value = info.modbus[i].value;
                        } else if (info.modbus[i].key == 203) {
                            console.log(info.modbus[i].value);
                            document.getElementsByName('bloodoxygen')[0].value = info.modbus[i].value;
                        }
                    }
                }
            };
            xhr.send(JSON.stringify(post_req));
        }

        // 启动循环上报
        function startAutoRefresh() {
            // 如果已经有定时器在运行，先清除它
            if (refreshIntervalId) {
                clearInterval(refreshIntervalId);
            }
            // 设置新的定时器
            refreshIntervalId = setInterval(refresh, currentInterval);
            document.getElementById('autoRefreshStatus').textContent = '自动刷新中';
            document.getElementById('autoRefreshStatus').classList.add('active');
            console.log(`循环上报已启动，间隔: ${currentInterval}毫秒`);
        }
        
        // 停止循环上报
        function stopAutoRefresh() {
            if (refreshIntervalId) {
                clearInterval(refreshIntervalId);
                refreshIntervalId = null;
                document.getElementById('autoRefreshStatus').textContent = '已停止';
                document.getElementById('autoRefreshStatus').classList.remove('active');
                console.log('循环上报已停止');
            }
        }

        // 更新上报间隔
        function updateRefreshInterval() {
            const intervalInput = document.getElementById('refreshInterval');
            const seconds = parseInt(intervalInput.value);
            
            if (isNaN(seconds) || seconds < 1) {
                alert('请输入有效的秒数（≥1）');
                intervalInput.value = currentInterval / 1000;
                return;
            }
            
            currentInterval = seconds * 1000; // 转换为毫秒
            localStorage.setItem('healthRefreshInterval', currentInterval); // 保存到本地存储
            
            // 如果循环上报已经开启，则重新启动定时器
            if (refreshIntervalId) {
                startAutoRefresh();
            }

            console.log(`上报间隔已设置为 ${seconds} 秒`);
        }

        // 切换自动刷新状态
        function toggleAutoRefresh() {
            if (refreshIntervalId) {
                stopAutoRefresh();
            } else {
                startAutoRefresh();
            }
        }
    </script>
</head>

<body>
    <div class="dashboard">
        <h1 class="title">居家健康与安全监测系统</h1>
        <div class="data-panel">
            <div class="data-item">
                <div class="data-label">心率脉搏 (bpm)</div>
                <input type="text" name="heartpulse" value="0" readonly>
            </div>

            <div class="data-item">
                <div class="data-label">光照强度 (Lux)</div>
                <input type="text" name="lightpower" value="0" readonly>
            </div>

            <div class="data-item">
                <div class="data-label">血氧浓度 (%)</div>
                <input type="text" name="bloodoxygen" value="0" readonly>
            </div>
        </div>
        <button type="button" class="btn" onclick="refresh()">数据刷新</button>
        <!-- 新增的循环上报控制区域 -->
        <div class="refresh-controls">
            <div>
                <button class="btn btn-sm" onclick="updateRefreshInterval()">设置间隔</button>
                <input type="number" id="refreshInterval" class="refresh-interval" min="1" value="3" placeholder="秒">
            </div>
            <div>
                <button class="btn btn-sm" onclick="toggleAutoRefresh()">切换循环上报</button>
                <span id="autoRefreshStatus" class="status-indicator">循环上报中</span>
            </div>
        </div>
        <div class="status-indicator">最后上报: <span id="lastRefreshTime">-</span></div>

        <div class="nav-links">
            <a href="mainpage.html">主页</a>
            <a href="stm32.html">STM32</a>
            <a href="modbusslave.html">Modbus Slave</a>
            <a href="modeselect.html">模式选择</a>
            <a href="http://192.168.43.241:8080/stream_simple.html">视频监控</a>
        </div>
    </div>
</body>

</html>